class Surveys::JTestsManageController < ApplicationController
   before_filter :auth
  # GET /tests
  # GET /tests.xml
    def index
      @tests = Survey.all
      @ts=[]
      for @test in @tests
        if @test.t_type==TEST_TYPE_JUMP
         @ts << @test
        end
      end

      respond_to do |format|
        format.html # index.html.erb
        format.xml  { render :xml => @ts }
      end
   end
   
  # 添加新的测试
  def add_new
    @test = Survey.new

    @result_quest = Array.new
    (0..2).each {@result_quest << Question.new}

    @choices
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @test }
    end
  end

  # 重新编辑测试信息
  def edit
    @test = Survey.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @test }
    end
  end
  
  # 展现 测试信息
  def show
    @test = Survey.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @test }
    end
  end
  
  # 更新测试信息
  def  update
    @test = Survey.find(params[:id])

    respond_to do |format|
      if @test.update_attributes(params[:test])
        flash[:notice] = 'Test was successfully updated.'
        format.html { redirect_to(@test) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @test.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # 删除一条测试
  def  destory
    @test = Survey.find(params[:id])
    @test.destroy

    respond_to do |format|
      format.html { redirect_to :controller => :j_test_manage }
      format.xml  { head :ok }
    end
  end


  
  # 创建测试 
  def create
   @test = Survey.new(params[:test])
   @test.t_type=TEST_TYPE_JUMP
   @test.professional = false
   #@Questions=Question.new[:results]
    respond_to do |format|
      if @test.save
          #----   
         for @value in params[:results]
           q=Question.new(@value)
           q.test_id=@test.id
           q.is_result= -255
           q.save
         end

        flash[:notice] = 'Test was successfully created.'
        format.html { redirect_to :controller => :j_test_manage } 
        format.xml  { render :xml => @test, :status => :created, :location => @test }
      else
        format.html { render :action => "add_new" }
        format.xml  { render :xml => @test.errors, :status => :unprocessable_entity }
      end
    end
  end
  #为跳转测试添加问题，跳转到的最后一个或者几个问题就是结果
  def new_question
    @test = Survey.find(params[:id])

    #生成几个空question
    @questions = Array.new
    (0..0).each {@questions << Question.new}
   # @choices = Array.new
   # (0..3).each { @choices << Choice.new}
      #生成choices 数组
     @choices=Array.new
      #为每一个题目生成几个 选项
      for @question in @questions
      (0..3).each{
       @choice = Choice.new
       @choice.question_id = @question.id
       @choices << @choice
       }
       end
    respond_to do |format|
      format.html 
      format.xml  { render :xml => @test }
    end
  end
  


  def create_question
     #取出test
     @test = Survey.find(params[:test][:id])
    
     respond_to do |format|
      begin
        #批量保存
        params[:question].each do |value|      
          q = Question.new(value)
             params[:choice].each do |element|
              c= Choice.new(element)
              c.question = q
              c.save
             end
          q.test = @test      
          q.save     
        end
        flash[:notice] = 'Test was successfully created.'
        format.html { redirect_to  :controller => :j_test_manage  }
        format.xml  { render :xml => @test, :status => :created, :location => @test }
      rescue
        format.html { render :action => "new_question" }
        format.xml  { render :xml => @test.errors, :status => :unprocessable_entity }
    # 完成了题目的添加功能 只是每次只能添加一道题目 2009 08 07 10:00
      end
    end 
  end

end
